package com.hilaver.dzmis.web.servlet.log;

import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.log.Log;
import com.hilaver.dzmis.service.impl.LogServiceImpl;
import com.hilaver.dzmis.web.servlet.AbstractBaseServlet;

public class LogServlet extends AbstractBaseServlet{
	
	
	public final static int ACTION_GET_ALL_UNSUCCESSFUL = 100;
	
	public final static int ACTION_GET_ALL_ILLEGAL_TIME = 101;
	
	public final static int ACTION_GET_ALL_ILLEGAL_ZONE = 102;
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public LogServlet(){
		this.fullName = Log.class.getName();
		this.simpleName = Log.class.getSimpleName();
	}
	
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			writer.write(execute(request));
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}

	protected String execute(HttpServletRequest request) throws Exception {
		this.setAction(request);

		LogServiceImpl serviceObj = (LogServiceImpl)super.getService();
		
		Method method = null;
		String rtn = "";
		Locale locale = (Locale) request.getSession().getAttribute(
				Constants.SESSION_LOCALE);
		
		method = serviceObj.getClass().getMethod("setLocale",
				new Class[] { Locale.class});
		method.invoke(serviceObj, new Object[] { locale });
		
		switch (this.action) {
		case ACTOIN_GET_ALL_PAGINATION: // "getAllPagination";
			this.setPaginationParam(request);
			method = serviceObj.getClass().getMethod("getAllPagination",
					new Class[] { int.class, String.class, String.class });// int
			// page,
			// String
			// sort,
			// String
			// order
			rtn = (String) method.invoke(serviceObj, new Object[] { this.page,
					this.sort, this.dir });
			break;
		case ACTION_EDIT: // "edit";
			this.fillObj(request);
			method = serviceObj.getClass().getMethod("edit",
					new Class[] { Object.class });
			rtn = (String) method.invoke(serviceObj, new Object[] { this.obj });
			break;
		case ACTION_DELETE: // "delete";
			this.setId(request);
			method = serviceObj.getClass().getMethod("delete",
					new Class[] { int.class });
			rtn = (String) method.invoke(serviceObj, new Object[] { this.id });
			break;
		case ACTION_GET: // "get one"
			this.setId(request);
			method = serviceObj.getClass().getMethod("get",
					new Class[] { int.class });
			rtn = (String) method.invoke(serviceObj, new Object[] { this.id });
			break;
		case ACTION_GET_ALL_PAGINATION_FILTER: // "getAllPagination filter"
			this.setPaginationParam(request);
			this.setFilterParam(request);
			method = serviceObj.getClass().getMethod(
					"getAllPagination",
					new Class[] { int.class, String.class, String.class,
							String[].class });// int page, String sort, String
			// order
			rtn = (String) method.invoke(serviceObj, new Object[] { this.page,
					this.sort, this.dir, this.filters });
			break;
		case ACTION_GET_ALL:
			this.setPaginationParam(request);
			method = serviceObj.getClass().getMethod("getAll",
					new Class[] { String.class, String.class });// String sort,
																// String
			// order
			rtn = (String) method.invoke(serviceObj, new Object[] { this.sort,
					this.dir });
			break;
		case ACTION_GET_ALL_UNSUCCESSFUL:
			this.setPaginationParam(request);
			this.setFilterParam(request);
			
			if(this.filters == null){
				this.filters = new String[]{"isSuccessful = '02'"};
			}else{
				String[] temp = new String[this.filters.length + 1];
				temp[0] = "isSuccessful = '02'";
				for(int i = 1; i < temp.length; i ++){
					temp[i] = this.filters[i -1];
				}
				this.filters = temp;
			}
			
			method = serviceObj.getClass().getMethod(
					"getAllPagination",
					new Class[] { int.class, String.class, String.class,
							String[].class });
			
			rtn = (String) method.invoke(serviceObj, new Object[] { this.page,
					this.sort, this.dir, this.filters });
			
			break;
			
		case ACTION_GET_ALL_ILLEGAL_TIME:
			this.setPaginationParam(request);
			this.setFilterParam(request);
			rtn = serviceObj.getIllegalTimePagination(page, sort, dir, filters);
			
			break;
			
		case ACTION_GET_ALL_ILLEGAL_ZONE:
			this.setPaginationParam(request);
			this.setFilterParam(request);
			
			rtn = serviceObj.getIllegalZonePagination(page, sort, dir, filters);
			
			break;
		
		}

		return rtn;
	}

}
